package com.xigua.xcar.common;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;

import com.xigua.xcar.R;
import com.xigua.xcar.base.BaseActivity;
import com.xigua.xcar.constant.Constants;
import com.xigua.xcar.data.CommonListData;
import com.xigua.xcar.phoneindex.PhoneIndexActivity;
import com.xigua.xcar.phonenumlist.PhoneNumListActivity;
import com.xigua.xcar.widgets.CommonList.CommonListAdapter;

public class CommonActivity extends BaseActivity {
	private static final String tag = "CommonActivity";
	
//	private MKSearch mSearch = new MKSearch();
//	private MKAddrInfo mAddrInfo = null;
//	private boolean mIsDevelop = true;
//	LocationListener mLocationListener = null;//create时注册此listener，Destroy时需要Remove
	
	private ListView mCommonList = null;
	private CommonListAdapter mCommonAdapter = null;
	private ArrayList<CommonListData> mArray = null;
	private static final int JINJI = 0;
	private static final int BAOXIAN = 1;
	private static final int SHOUHOU = 2;
	private OnClickListener mListener = new OnClickListener() {

		public void onClick(View v) {
			switch (v.getId()) {
			case JINJI: {// 紧急常用
				Intent intent = new Intent(CommonActivity.this,
						PhoneNumListActivity.class);
				intent.putExtra(Constants.COMMON_FROMWHERE, 0);
				CommonActivity.this.startActivity(intent);
				break;
			}
			case BAOXIAN: {// 保险客服
				Intent intent = new Intent(CommonActivity.this,
						PhoneNumListActivity.class);
				intent.putExtra(Constants.COMMON_FROMWHERE, 1);
				CommonActivity.this.startActivity(intent);
				break;
			}
			case SHOUHOU: {// 4S店
				Intent intent = new Intent(CommonActivity.this,
						PhoneIndexActivity.class);
				CommonActivity.this.startActivity(intent);
				break;
			}
			}
		}
	};

	@Override
	protected void finalize() throws Throwable {
		// 注销的注意事项
		super.finalize();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setView(R.layout.common);
		setTopBarTitle("常用");
		setTitleBar(0, "", 0, "");
		mArray = new ArrayList<CommonListData>();
		initData();
		mCommonList = (ListView) findViewById(R.id.listview_common_main);
		mCommonAdapter = new CommonListAdapter(this);
		mCommonAdapter.setItemClickListener(mListener);
		mCommonAdapter.setData(mArray);
		mCommonList.setAdapter(mCommonAdapter);
		
//		BMapApiDemoApp app = (BMapApiDemoApp)getApplication();
//		mIsDevelop = app.mIsDevelopMode;
//		if(false == mIsDevelop){
//			if (app.mBMapMan == null) {
//				
//			}else{
//				app.mBMapMan.destroy();
//			}
//			app.mBMapMan = new BMapManager(getApplication());
//			app.mBMapMan.init(app.mStrKey, new BMapApiDemoApp.MyGeneralListener());
//			mSearch.init(app.mBMapMan, new MKSearchListener() {
//				
//				public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1) {
//					// TODO Auto-generated method stub
//					
//				}
//				
//				public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) {
//					// TODO Auto-generated method stub
//					
//				}
//				
//				public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2) {
//					// TODO Auto-generated method stub
//					
//				}
//				
//				public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1) {
//					// TODO Auto-generated method stub
//					
//				}
//				
//				public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {
//					// TODO Auto-generated method stub
//					
//				}
//				
//				public void onGetAddrResult(MKAddrInfo arg0, int arg1) {
//					if(arg0==null){
//						Log.i(tag,"onGetAddrResult:address:!!!null!!!");
//					}else{
//						if(null == mAddrInfo){
//							mAddrInfo = arg0;
//							Log.i(tag,"onGetAddrResult:address:"+mAddrInfo.strAddr);
////							Toast.makeText(NearbyActivity.this, mAddrInfo.strAddr, 3000).show();
//						}else{
//							if(mAddrInfo.strAddr.equals(arg0.strAddr)){
//								return ;
//							}
//							mAddrInfo = arg0;
//							Log.i(tag,"onGetAddrResult:address:"+mAddrInfo.strAddr);
////							Toast.makeText(NearbyActivity.this, mAddrInfo.strAddr, 3000).show();
//						}
//					}
//				}
//
//				@Override
//				public void onGetSuggestionResult(MKSuggestionResult arg0,
//						int arg1) {
//					// TODO Auto-generated method stub
//					
//				}
//			});
////			app.mBMapMan.start();
//			
//			mLocationListener = new LocationListener(){
//				
//				public void onLocationChanged(Location location) {
//					if(location != null){
//						String strLog = String.format("您当前的位置:\r\n" +
//								"纬度:%f\r\n" +
//								"经度:%f",
//								location.getLongitude(), location.getLatitude());
//						Log.i(tag,strLog);
//						RoutePlan.setLocation(location);
//					}else{
//						Log.i(tag,"!!!null!!!");
//					}
//				}
//				
//				
//	        };

//		}
	}

	private void initData() {
		CommonListData data = new CommonListData();
		data.setmShopName("紧急常用");
		data.setmResId(R.drawable.icon_rightarray);
		mArray.add(data);
		data = new CommonListData();
		data.setmShopName("保险客服");
		data.setmResId(R.drawable.icon_rightarray);
		mArray.add(data);
		data = new CommonListData();
		data.setmShopName("4S客服");
		data.setmResId(R.drawable.icon_rightarray);
		mArray.add(data);
	}

	@Override
	protected void handleTitleBarEvent(int buttonId) {
		switch (buttonId) {
		case Constants.LEFTBUTTONID: {
			break;
		}
		case Constants.RIGHTBUTTONID: {
			break;
		}
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		Log.d(tag, "back");
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { //按下的如果是BACK，同时没有重复
	    	 AlertDialog.Builder builder = new AlertDialog.Builder(this);  
	    	    builder.setTitle("提示");  
	    	    builder.setMessage("确定要退出程序吗?");  
	    	    builder.setPositiveButton("确定",  
	    	            new DialogInterface.OnClickListener() {  
	    	                public void onClick(DialogInterface dialog, int whichButton) {           
	    	                	CommonActivity.this.finish();  
	    	                }  
	    	            });  
	    	    builder.setNegativeButton("取消",  
	    	            new DialogInterface.OnClickListener() {  
	    	                public void onClick(DialogInterface dialog, int whichButton) {  
	    	                }  
	    	            }).create(); 
	    	
	    	    builder.show();
	    	    
	        return true;
	    }
		return super.onKeyDown(keyCode, event);
	}
	
	@Override
	protected void onPause() {
    	Log.i(tag, "onPause");
//    	if(false == mIsDevelop){
//    		BMapApiDemoApp app = (BMapApiDemoApp)this.getApplication();
//    		// 移除listener
//    		app.mBMapMan.stop();
//    	}
		super.onPause();
	}
    
	@Override
	protected void onResume() {
		Log.i(tag, "onResume");
//		if(false == mIsDevelop){
//			BMapApiDemoApp app = (BMapApiDemoApp)this.getApplication();
//			// 注册Listener
//	        app.mBMapMan.getLocationManager().requestLocationUpdates(mLocationListener);
//			app.mBMapMan.start();
//		}
		super.onResume();
	}
}
